package com.a214.yxz.yuejian013.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.a214.yxz.yuejian013.Fragment.UserOrderSub1Fragment;
import com.a214.yxz.yuejian013.Fragment.UserOrderSub2Fragment;
import com.a214.yxz.yuejian013.Fragment.UserOrderSub3Fragment;
import com.a214.yxz.yuejian013.Fragment.UserOrderSub4Fragment;
import com.a214.yxz.yuejian013.Fragment.UserOrderSub5Fragment;
import com.a214.yxz.yuejian013.R;

import java.util.ArrayList;
import java.util.List;

public class UserOrderActivity extends AppCompatActivity {

    private ImageButton return_btn;
    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private FragmentPagerAdapter fragmentPagerAdapter;
    private RadioGroup radioGroup;
    private UserOrderSub1Fragment sub1Fragment;
    private UserOrderSub2Fragment sub2Fragment;
    private UserOrderSub3Fragment sub3Fragment;
    private UserOrderSub4Fragment sub4Fragment;
    private UserOrderSub5Fragment sub5Fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_order);

        return_btn = (ImageButton) findViewById(R.id.user_order_return_btn);
        viewPager= (ViewPager) findViewById(R.id.user_order_viewPager);
        radioGroup= (RadioGroup) findViewById(R.id.user_order_radioGroup);

        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        sub1Fragment = new UserOrderSub1Fragment();
        sub2Fragment = new UserOrderSub2Fragment();
        sub3Fragment = new UserOrderSub3Fragment();
        sub4Fragment = new UserOrderSub4Fragment();
        sub5Fragment = new UserOrderSub5Fragment();
        fragmentList.add(sub1Fragment);
        fragmentList.add(sub2Fragment);
        fragmentList.add(sub3Fragment);
        fragmentList.add(sub4Fragment);
        fragmentList.add(sub5Fragment);

        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                switch (viewPager.getCurrentItem()){
                    case 0:
                        radioGroup.check(R.id.user_order_R_allOrder);
                        break;
                    case 1:
                        radioGroup.check(R.id.user_order_R_unfinishOrder);
                        break;
                    case 2:
                        radioGroup.check(R.id.user_order_R_finishOrder);
                        break;
                    case 3:
                        radioGroup.check(R.id.user_order_R_revokingOrder);
                        break;
                    case 4:
                        radioGroup.check(R.id.user_order_R_revokedOrder);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch(i){
                    case R.id.user_order_R_allOrder:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.user_order_R_unfinishOrder:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.user_order_R_finishOrder:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.user_order_R_revokingOrder:
                        viewPager.setCurrentItem(3);
                        break;
                    case R.id.user_order_R_revokedOrder:
                        viewPager.setCurrentItem(4);
                        break;
                }
            }
        });
        RadioButton radioButton= (RadioButton) findViewById(R.id.user_order_R_allOrder);
        radioButton.setChecked(true);
    }
}
